<script>
import _axios from "@/plugins/axios";
import request from "@/utils/request";
//import Aside from "@/components/Aside.vue";
//import Header from "@/components/Header.vue";
//import Manager from "./Manager.vue";
export default {
  name: "Pet",
  data() {
    return {
      user:localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      id: '',
      nowID:'',
      typeSeller:false,
      dialogFormVisible: false,
      formLabelWidth:'100px',
      form: {
        name: '',
        type: '',
        sellerId: '',
        petPrice: '',
        status: '',
        imgUrl: ''
      },
      fits: [''],
      multipleSelection: [],
      options: [{
        value: '猫',
        label: '猫'
      }, {
        value: '狗',
        label: '狗'
      }, {
        value: '兔子',
        label: '兔子'
      }],
    }
  },
  created() {
    //请求分页查询数据
    this.load()
  },
  //  components: {
  //   Aside,
  //   Header,
  //   Manager
  // },
  methods: {
    reset() {
      this.id = ""
      this.load()
    },
    load() {
      request.post("/Pet/getPetPage", {
              pageNo: this.pageNum,
              pageSize: this.pageSize,
              id: this.id
          }
      ).then(res => {
        console.log(res)
        this.tableData = res.data.result
        this.total = res.data.total

      })
    },
    loadForOne() {
      request.post("/Pet/query", {
            pageNo: this.pageNum,
            pageSize: this.pageSize,
            id: this.id
          }
      ).then(res => {
        console.log(res)
        this.tableData = res.data.result
        this.total = res.data.total
      })
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    save() {
      this.load()
      if(!this.nowID) {
        request.post("/Pet/insert", this.form).then(res => {
          if (res) {
            this.$message.success("保存成功")
            this.dialogFormVisible = false
            this.load()
          } else {
            this.$message.error("保存失败")
          }

        })
      }
      else{
        request.post("/Pet/update", this.form,this.nowID).then(res => {
          if (res) {
            this.$message.success("保存成功")
            this.dialogFormVisible = false
            this.load()
          } else {
            this.$message.error("保存失败")
          }

        })
      }
    },
    handleEdit(row) {
      this.form = row
      console.log('Editing row:', row);
      this.dialogFormVisible = true
      this.form = Object.assign({}, row)
      this.nowID=row.id
    },
    handleDelete(row) {
      request.post("/Pet/delete" ,{
        id:row.id
      }).then(res => {
        if (res) {
          this.$message.success("删除成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleTransfer(row) {
      this.nowID=this.user.id
      console.log('row:', row);
      request.post("/User/transfer",{
        buyerId:this.nowID,
        sellerId:row.sellerId,
        petId:row.id,
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    isSeller(row){
      if(row.sellerId==this.user.id){
        this.typeSeller=true
      }
      else{
        this.typeSeller=false
      }
    }
    /*deleteBatch(){
      let ids= this.multipleSelection.map(v =>v.name)//把对象数组变成纯name的数组
      _axios.post("/Pet/del/batch",ids).then(res=>{
        if(res){
          this.$message.success("批量删除成功")
          this.dialogFormVisible=false
          this.load()
        }
        else {
          this.$message.error("批量删除失败")
        }
      })
    },*/
  }
}
</script>

<template >
  <div class="PetView">
    <div style="margin-bottom: 30px">

    </div>

    <div style="padding: 10px 0">
      <el-input v-model="id" style="width: 200px" placeholder="请输入宠物id" suffix-icon="el-icon-zoom-in"></el-input>
      <!--            <el-button style="margin-left: 5px" type="primary" @click="load">搜索</el-button>-->
      <!--            <el-input v-model="research_direction"style="width: 200px;margin-left: 5px" placeholder="请输入研究方向" suffix-icon="el-icon-office-building"></el-input>-->
      <!--            <el-button style="margin-left: 5px" type="primary" @click="load">搜索</el-button>-->
      <!--            <el-input v-model="work_place"style="width: 200px;margin-left: 5px" placeholder="请输入办公室" suffix-icon="el-icon-office-building"></el-input>-->
      <el-button style="margin-left: 5px" type="primary" @click="loadForOne">搜索</el-button>
      <el-button style="margin-left: 5px" type="info" @click="reset">重置</el-button>
    </div>

    <div style="margin: 8px 0">
      <el-button type="primary" @click="handleAdd">上架宠物<i class="el-icon-circle-plus" style="margin-left: 10px"></i></el-button>
    </div>
    <div class="container">
    <el-table :data="tableData" border stripe @selection-change="handleSelectionChange">
      <el-table-column prop="id" label="宠物id" width="70">
      </el-table-column>
      <el-table-column prop="name" label="名称" width="100">
      </el-table-column>
      <el-table-column prop="type" label="宠物品类" width="120">
      </el-table-column>
      <el-table-column prop="sellerId" label="卖家" width="100">
      </el-table-column>
      <el-table-column prop="petPrice" label="价格" width="90">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="90">
      </el-table-column>
      <el-table-column prop="imgUrl" label="图片" width="200">
        <template #default="scope">
          <el-image :src="scope.row.imgUrl" style="width:150px"></el-image>
        </template>
      </el-table-column>
      <el-table-column>
        <template slot-scope="scope">
          <el-popconfirm
              style="margin-left: 5px"
              confirm-button-text='确定'
              cancel-button-text='再想想'
               icon="el-icon-info"
               icon-color="red"
              title="确定购买吗？"
              @confirm="handleTransfer(scope.row)"
          >
            <el-button v-if="scope.row.sellerId != user.id&&scope.row.status=='在售'" type="success" slot="reference">购买<i class="el-icon-shopping-cart-full"></i></el-button>
          </el-popconfirm>
          <el-button  v-if="scope.row.sellerId == user.id" type="primary" @click="handleEdit(scope.row)" >编辑<i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              style="margin-left: 5px"
              confirm-button-text='确定'
              cancel-button-text='再想想'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗？"
              @confirm="handleDelete(scope.row)"
          >
            <el-button v-if="scope.row.sellerId == user.id" type="danger" slot="reference">删除<i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!-- Form -->
    <el-dialog title="宠物信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form lable-width="100px">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="宠物品类" :label-width="formLabelWidth">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="卖家" :label-width="formLabelWidth">
          <el-select v-model="form.sellerId" placeholder="卖家id">
            <el-option :label="user.id" :value="user.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="价格" :label-width="formLabelWidth">
          <el-input v-model="form.petPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio v-model="form.status" label="在售">在售</el-radio>
          <el-radio v-model="form.status" label="暂停出售">暂停出售</el-radio>
        </el-form-item>
        <el-form-item label="图片链接" :label-width="formLabelWidth">
          <el-input v-model="form.imgUrl" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
  </div>
</template>

<style scoped>
.container {
  height: 100%;
  position: relative;
  padding-bottom: 50px; /* 底部留出足够的空间来容纳固定的元素 */
}
.PetView{
 height: 100vh;
}
.el-pagination {
  position: fixed;
  bottom: 0;
}
</style>